//
//  CardToolCell.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/5.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol CardToolCellDelegate {
    @objc optional func selectType(title:String)
}

class CardToolCell: UITableViewCell {
    
    fileprivate let cellId = "cardTypeCell"
    
    weak var toolDelegate : CardToolCellDelegate?
    
    var dataArray = [String](){
        didSet{
            collectionView.reloadData()
        }
    }
    
    var collectionView : UICollectionView!
    
    let titleLabel = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = UIColor.white
        
        let imageView = UIImageView()
        if KMTool.isSignIn() {
            imageView.image = KMTool.base64ToImage(base: KMTool.stringForEnumKey(.avatar) ?? "")
        }else{
            imageView.image = #imageLiteral(resourceName: "avatar_default")
        }
        
        imageView.layer.cornerRadius = RATIO_H(maxNum: 15)
        imageView.layer.masksToBounds = true
        self.contentView.addSubview(imageView)
        
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(titleLabel)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = RATIO_H(maxNum: 10)
        layout.minimumInteritemSpacing = RATIO(maxNum: 10)
        layout.itemSize = CGSize.init(width: RATIO(maxNum: 100), height: RATIO_H(maxNum: 30))
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(CardTypeCell.self, forCellWithReuseIdentifier: cellId)
        self.contentView.addSubview(collectionView)
        
        imageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 8))
            make.left.equalToSuperview().offset(RATIO(maxNum: 8))
            make.width.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(imageView.snp.right).offset(RATIO(maxNum: 10))
            make.centerY.equalTo(imageView)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel).offset(RATIO(maxNum: 10))
            make.top.equalTo(imageView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.width.equalTo(RATIO(maxNum: 220))
            make.bottom.equalToSuperview().offset(RATIO_H(maxNum: -10))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension CardToolCell : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? CardTypeCell
        cell?.contentView.layer.cornerRadius = RATIO_H(maxNum: 15)
        cell?.contentView.layer.masksToBounds = true
        cell?.titleLabel.text = dataArray[indexPath.row]
        return cell!
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.toolDelegate?.selectType!(title: dataArray[indexPath.row])
    }

    
    
}
